#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx

class ImagePanel(wx.Panel):
    def __init__(self,parent,size=(150,150),border=3):
        wx.Panel.__init__(self,parent,size=size)
        self.border=border
        self.sb=wx.StaticBitmap(self)
        self.sb.Bind(wx.EVT_LEFT_DOWN,self.OnClick)
        self.SetBitmap(wx.EmptyBitmap(0,0))
        
        
    def SetImage(self,img):
        self.bitmap=wx.BitmapFromImage(img)
        self.ReDraw()
        
    def SetBitmap(self,bmp):
        self.bitmap=bmp
        self.ReDraw()
        
    def SetStream(self,stream):
        img = wx.ImageFromStream(stream)
        self.bitmap=wx.BitmapFromImage(img)
        self.ReDraw()
        
    def SetFile(self,filename):
        self.bitmap=wx.Bitmap(filename)
        self.ReDraw()
        
    def ReDraw(self):
        w,h=self.GetClientSize()
        img=wx.ImageFromBitmap(self.bitmap)
        bmp=wx.BitmapFromImage(img.Scale(w,h))
        
        self.sb.SetBitmap(wx.NullBitmap)
        self.sb.SetBitmap(bmp)
        self.sb.SetPosition((self.border,self.border))
        
        self.Refresh()
        
    def SetBorder(self,b):
        self.border=b
        self.ReDraw()       
        
    def GetClientSize(self):
        w,h=self.GetSize()
        return w-2*self.border,h-2*self.border
        
    def GetEmptyBitmap(self,color=wx.WHITE):
        bmp=wx.EmptyBitmap(150,150)
        dc=wx.MemoryDC(bmp)
        dc.SetBackground(wx.Brush(color))
        dc.Clear()
        dc.SelectObject(wx.NullBitmap)
        return bmp
        
    def OnClick(self,event):
        self.AddPendingEvent(event)
        
class TestFrame1(wx.Frame):
    
    def __init__(self):
        wx.Frame.__init__(self,parent=None,id=wx.ID_ANY,title=u"儿童英语",size=(800,600))
        panel=wx.Panel(self)
        btn=wx.Button(panel,-1,"ok",pos=(650,450))
        btn.Bind(wx.EVT_BUTTON,self.OnClick)
        self.p=ImagePanel(panel)
        self.p.SetBackgroundColour(wx.WHITE)
        
        self.p.Bind(wx.EVT_LEFT_DOWN,self.OnClickP)
        
    def OnClick(self,evt):
        self.p.SetBorder(30)
        self.p.Refresh()
        
    def OnClickP(self,evt):
        self.p.SetFile("d:\\1.jpg")
        
if __name__=='__main__':
    #初始化程序
    app=wx.App(False)
    frm=TestFrame1()
    frm.Center()
    frm.Show()
    #开始消息循环
    app.MainLoop()